/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-31下午08:36:59
 */
package hit.edance.campusmobile.chat.model;

import java.util.HashMap;

import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;

import hit.edance.campusmobile.chat.activity.ActivityChat;
import hit.edance.campusmobile.chat.activity.ActivityTab;
import hit.edance.campusmobile.chat.listener.ChatManagerListenerEx;
import hit.edance.campusmobile.chat.service.CampusMobileService;

/**   
 * 聊天管理
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.model
 * @Class MyChatManager
 * @Date 2012-5-31 下午08:36:59
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class MyChatManager {
	private static MyChatManager instance;
	private static ActivityChat currentChattingActivity;
	private static HashMap<String, ChatEntity> chatListManager = new HashMap<String, ChatEntity>();
	private static ActivityTab activityTab;

	private static ChatManager chatmanager;
	
	public static MyChatManager getInstance(){
		if(instance == null)
			instance = new MyChatManager();
		return instance;
	}
	public static void startListener(){
		if(chatmanager != null && !chatmanager.getChatListeners().isEmpty()){
			for(ChatManagerListener listener : chatmanager.getChatListeners()){
				chatmanager.removeChatListener(listener);
			}
		}
		if(instance == null)
			instance = new MyChatManager();
		chatmanager.addChatListener(new ChatManagerListenerEx());
		
	}
	private MyChatManager(){
		chatmanager = CampusMobileService.connection.getChatManager();
		chatmanager.addChatListener(new ChatManagerListenerEx());
	}
	
	/**
	 * @return the chatmanager
	 */
	public static ChatManager getChatmanager() {
		return chatmanager;
	}

	/**
	 * @return the currentChattingActivity
	 */
	public static ActivityChat getCurrentChattingActivity() {
		return currentChattingActivity;
	}

	/**
	 * @param currentChattingActivity the currentChattingActivity to set
	 */
	public static void setCurrentChattingActivity(
			ActivityChat currentChattingActivity) {
		MyChatManager.currentChattingActivity = currentChattingActivity;
	}

	/**
	 * @return the chatListManager
	 */
	public static HashMap<String, ChatEntity> getChatListManager() {
		return chatListManager;
	}

	/**
	 * @param chatListManager the chatListManager to set
	 */
	public static void setChatListManager(
			HashMap<String, ChatEntity> chatListManager) {
		MyChatManager.chatListManager = chatListManager;
	}
	public static ChatEntity getChatEntityByJID(String JID){
		return chatListManager.get(JID);
	}
	public static ChatEntity addChatEntity(ChatEntity chatEntity){
		chatListManager.put(chatEntity.getJabberId(),chatEntity);
		return chatEntity;
	}

	/**
	 * @return the activityTab
	 */
	public static ActivityTab getActivityTab() {
		return activityTab;
	}

	/**
	 * @param activityTab the activityTab to set
	 */
	public static void setActivityTab(ActivityTab activityTab) {
		MyChatManager.activityTab = activityTab;
	}
	/* @see java.lang.Object#finalize()
	 *finalize
	 */
	public static void close(){
		if(chatmanager != null && !chatmanager.getChatListeners().isEmpty()){
			for(ChatManagerListener listener : chatmanager.getChatListeners()){
				chatmanager.removeChatListener(listener);
			}
		}
		currentChattingActivity = null;
		chatListManager.clear();
		activityTab = null;
	}

}
